//
//  UILabel+LCExtension.m
//  LCPackage
//
//  Created by 马军凯 on 2021/6/22.
//  Copyright © 2021 龙采科技集团. All rights reserved.
//

#import "UILabel+LCExtension.h"
#import "UIView+LCExtension.h"

@implementation UILabel (LCExtension)

- (CGSize)LC_attrText:(NSAttributedString *)text Auto:(LC_Direction)direction {
    return [self LC_attrText:text numberOfLines:0 Auto:direction];
}

- (CGSize)LC_text:(NSString *)text Auto:(LC_Direction)direction {
    return [self LC_text:text numberOfLines:0 Auto:direction];
}

- (CGSize)LC_text:(NSString *)text numberOfLines:(NSInteger)number Auto:(LC_Direction)direction {
    self.numberOfLines = number;
    self.lineBreakMode = NSLineBreakByTruncatingTail;
    self.text = text;
    
    CGSize size;
    if (direction == LC_Horizontal) {
        size = [self sizeThatFits:CGSizeMake(MAXFLOAT, self.frame.size.height)];
        self.width = size.width;
    } else {
        size = [self sizeThatFits:CGSizeMake(self.frame.size.width, MAXFLOAT)];
        self.height = size.height;
    }
    return size;
}

- (CGSize)LC_attrText:(NSAttributedString *)text numberOfLines:(NSInteger)number Auto:(LC_Direction)direction {
    self.numberOfLines = number;
    self.lineBreakMode = NSLineBreakByTruncatingTail;
    self.attributedText = text;
    
    CGSize size;
    if (direction == LC_Horizontal) {
        size = [self sizeThatFits:CGSizeMake(MAXFLOAT, self.frame.size.height)];
        self.width = size.width;
    } else {
        size = [self sizeThatFits:CGSizeMake(self.frame.size.width, MAXFLOAT)];
        self.height = size.height;
    }
    return size;
}

//改变label内某一个字段的颜色
- (void)changeLabelTitleColorWith:(NSString *)text textColor:(UIColor *)textColor {
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:self.text];
    NSRange range = NSMakeRange([[noteStr string] rangeOfString:text].location, [[noteStr string] rangeOfString:text].length);
    [noteStr setAttributes:@{NSForegroundColorAttributeName : textColor} range:range];
    self.attributedText = noteStr;
}


@end
